package com.zqxx.examine.management.controller.action.admin;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.zqxx.examine.management.security.UserInfo;
import com.zqxx.examine.management.service.QuestionService;
import com.zqxx.examine.management.service.UserService;
import com.zqxx.examine.model.exam.Message;
import com.zqxx.examine.model.question.Field;
import com.zqxx.examine.model.question.KnowledgePoint;
import com.zqxx.examine.model.question.Tag;
import com.zqxx.examine.model.user.Department;

@Controller
public class CommonActionAdmin
{
  @Autowired
  private QuestionService questionService;
  @Autowired
  private UserService userService;
  
  @RequestMapping(value={"/admin/common/field-add"}, method={org.springframework.web.bind.annotation.RequestMethod.POST})
  @ResponseBody
  public Message addField(@RequestBody Field field)
  {
    Message message = new Message();
    try
    {
      this.questionService.addField(field);
    }
    catch (Exception e)
    {
      message.setResult(e.getClass().getName());
      e.printStackTrace();
    }
    return message;
  }
  
  @RequestMapping(value={"/admin/common/point-add"}, method={org.springframework.web.bind.annotation.RequestMethod.POST})
  @ResponseBody
  public Message addPoint(@RequestBody KnowledgePoint point)
  {
    Message message = new Message();
    try
    {
      this.questionService.addKnowledgePoint(point);
    }
    catch (Exception e)
    {
      message.setResult(e.getClass().getName());
      e.printStackTrace();
    }
    return message;
  }
  
  @RequestMapping(value={"/admin/common/tag-add"}, method={org.springframework.web.bind.annotation.RequestMethod.POST})
  @ResponseBody
  public Message addTag(@RequestBody Tag tag)
  {
    UserInfo userInfo = (UserInfo)SecurityContextHolder.getContext().getAuthentication().getPrincipal();
    tag.setCreator(userInfo.getUserid());
    Message message = new Message();
    try
    {
      this.questionService.addTag(tag);
    }
    catch (Exception e)
    {
      message.setResult(e.getClass().getName());
      e.printStackTrace();
    }
    return message;
  }
  
  @RequestMapping(value={"/admin/common/dep-add"}, method={org.springframework.web.bind.annotation.RequestMethod.POST})
  @ResponseBody
  public Message addDep(@RequestBody Department dep)
  {
    Message message = new Message();
    try
    {
      this.userService.addDep(dep);
    }
    catch (Exception e)
    {
      message.setResult(e.getClass().getName());
      e.printStackTrace();
    }
    return message;
  }
  
  @RequestMapping(value={"/admin/common/get-knowledge-point/{fieldId}"}, method={org.springframework.web.bind.annotation.RequestMethod.GET})
  @ResponseBody
  public Message getQuestionPointByFieldId(@PathVariable int fieldId)
  {
    Message message = new Message();
    try
    {
      HashMap<Integer, String> pointMap = new HashMap();
      
      List<KnowledgePoint> pointList = this.questionService.getKnowledgePointByFieldId(fieldId, null);
      for (KnowledgePoint point : pointList) {
        pointMap.put(Integer.valueOf(point.getPointId()), point.getPointName());
      }
      message.setObject(pointMap);
    }
    catch (Exception e)
    {
      e.printStackTrace();
      message.setResult(e.getClass().getName());
    }
    return message;
  }
  
  @RequestMapping(value={"/admin/common/delete-field-{fieldId}"}, method={org.springframework.web.bind.annotation.RequestMethod.GET})
  @ResponseBody
  public Message deleteField(Model model, @PathVariable("fieldId") int fieldId)
  {
    List<Integer> idList = new ArrayList();
    idList.add(Integer.valueOf(fieldId));
    Message msg = new Message();
    try
    {
      this.questionService.deleteFieldByIdList(idList);
    }
    catch (Exception e)
    {
      e.printStackTrace();
      msg.setResult(e.getClass().getName());
    }
    return msg;
  }
  
  @RequestMapping(value={"/admin/common/delete-point-{pointId}"}, method={org.springframework.web.bind.annotation.RequestMethod.GET})
  @ResponseBody
  public Message deleteKnowledgePoint(Model model, @PathVariable("pointId") int pointId)
  {
    List<Integer> idList = new ArrayList();
    idList.add(Integer.valueOf(pointId));
    Message msg = new Message();
    try
    {
      this.questionService.deleteKnowledgePointByIdList(idList);
    }
    catch (Exception e)
    {
      e.printStackTrace();
      msg.setResult(e.getClass().getName());
    }
    return msg;
  }
  
  @RequestMapping(value={"/admin/common/tag-delete-{tagId}"}, method={org.springframework.web.bind.annotation.RequestMethod.GET})
  @ResponseBody
  public Message deleteTag(Model model, @PathVariable("tagId") int tagId)
  {
    List<Integer> idList = new ArrayList();
    idList.add(Integer.valueOf(tagId));
    
    Message msg = new Message();
    try
    {
      this.questionService.deleteTagByIdList(idList);
    }
    catch (Exception e)
    {
      e.printStackTrace();
      msg.setResult(e.getClass().getName());
    }
    return msg;
  }
  
  @RequestMapping(value={"/admin/common/delete-dep-{depId}"}, method={org.springframework.web.bind.annotation.RequestMethod.GET})
  @ResponseBody
  public Message deleteDep(Model model, @PathVariable("depId") int depId)
  {
    Message msg = new Message();
    try
    {
      this.userService.deleteDep(depId);
    }
    catch (Exception e)
    {
      e.printStackTrace();
      msg.setResult(e.getClass().getName());
    }
    return msg;
  }
}
